package com.seele.tigerwallet.ui.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.seele.tigerwallet.R;
import com.seele.tigerwallet.model.UserInfo;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.regex.Pattern;

public class SPUtils {

    /**
     * 保存在手机里面的文件名
     */
    public static final String FILE_NAME = "TigerWallet.prefs";

    private static Context mContext;

    public static final String IS_FIRST_LOGIN = "is_first_login";
    public static final String IS_SWITCH_WALLET = "is_switch_wallet";

    public static final String IS_FIRST_MARKET = "is_first_market";

    public static final String ETH_PRICE = "eth_price";
    public static final String ETH_BALANCE = "eth_balance";


    public static final String ASSET_SHOW = "asset_show";


    public static final String TRANSFER_TIME = "transfer_time";

    public static final String FINGERPRINTS = "FINGERPRINTS";
    private static Object lock = new Object();


    public static void setContext(Context context) {
        mContext = context;
    }

    /**
     * 保存String
     *
     * @param key
     * @param value
     */
    public static void putString(String key, String value) {
        SharedPreferences sp = mContext.getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, value);
        editor.apply();

    }


    /**
     * 保存String
     *
     * @param key
     * @param value
     */
    public static void putFloat(String key, Float value) {
        synchronized (lock){
            SharedPreferences sp = mContext.getSharedPreferences(FILE_NAME,
                    Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putFloat(key, value);
            editor.apply();

        }


    }

    public static float getFloat(String key, Float defaultValue) {
        synchronized (lock){
            SharedPreferences sp = mContext.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
            return sp.getFloat(key, defaultValue);
        }

    }

    /**
     * long
     *
     * @param key
     * @param value
     */
    public static void putLong(String key, long value) {
        SharedPreferences sp = mContext.getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putLong(key, value);
        editor.apply();

    }

    public static float getLong(String key, long defaultValue) {
        SharedPreferences sp = mContext.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sp.getLong(key, defaultValue);
    }


    /**
     * 保存String
     *
     * @param key
     * @param value
     */
    public static void putBoolean(String key, boolean value) {
        SharedPreferences sp = mContext.getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(key, value);
        editor.apply();

    }


    public static boolean getBoolean(String key, boolean defaultValue) {
        SharedPreferences sp = mContext.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sp.getBoolean(key, defaultValue);
    }


    /**
     * 获取值
     *
     * @param key
     * @return
     */
    public static String getString(String key) {
        String value = null;
        SharedPreferences var2 = mContext.getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        value = var2.getString(key, "");
        return value;
    }

    /**
     * 保存钱包配置
     *
     * @param userInfo
     */
    public static void saveUserInfo(UserInfo userInfo) {

        synchronized (lock) {
            if (null != userInfo) {
                putString(userInfo.getClass().getSimpleName(), GsonUtil.toJsonString(userInfo));
            }
        }
    }


    /**
     * 获取钱包配置
     *
     * @param userInfo
     * @return
     */
    public static UserInfo getUserInfo(UserInfo userInfo) {
        synchronized (lock) {
            if (null != userInfo) {
                String value = getString(userInfo.getClass().getSimpleName());
                if (!TextUtils.isEmpty(value)) {
                    userInfo = GsonUtil.parseJsonWithGson(value, userInfo.getClass());
                    return userInfo;
                }
            }
        }

        return userInfo;
    }

    /**
     * 获取资产单位
     *
     * @return
     */
    public static String getCompany() {
        if (UserInfo.get().company == 0) {
            String text = String.format(mContext.getString(R.string.assets_total), mContext.getString(R.string.usdt));
            return text;

        } else if (UserInfo.get().company == 1) {
            String text = String.format(mContext.getString(R.string.assets_total), mContext.getString(R.string.cny));
            return text;
        }
        return null;

    }


    /**
     * 获取资产单位
     *
     * @return
     */
    public static String getNewPrice() {
        if (UserInfo.get().company == 0) {
            String text = String.format(mContext.getString(R.string.new_price), mContext.getString(R.string.usdt));
            return text;

        } else if (UserInfo.get().company == 1) {
            String text = String.format(mContext.getString(R.string.new_price), mContext.getString(R.string.cny));
            return text;
        }
        return null;

    }

    /**
     * 判断是美元还是RMB
     * false 美元
     *
     * @return
     */
    public static boolean isCNY() {

        return 0 == UserInfo.get().company ? false : true;
    }


    /**
     * 是否是正确的地址 (0X,包括0X42位数,16进制,无空格)
     *
     * @return
     */
    public static boolean isAddress(String address) {

        return Pattern.matches("^0[xX][\\da-fA-F]{40}$", address);


    }


    public static String getGas(String minerfee) {
        float total;
        if (TextUtils.isEmpty(minerfee)){
            return String.format("%.8f", 0.00000000);
        }
        total=Float.parseFloat(minerfee);
        return String.format("%.8f", total);
    }


}
